﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GestureLib;

namespace GesturePad
{
    public partial class AddEditTrainedGestureForm : Form
    {
        public AddEditTrainedGestureForm()
        {
            InitializeComponent();
        }

        public TrainedGesture TrainedGesture { get; set; }
        public GestureLib.GestureLib GestureLib { get; set; }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (TrainedGesture == null)
            {
                TrainedGesture = new TrainedGesture();
            }

            TrainedGesture.Name = txtTrainedGestureName.Text;
            TrainedGesture.GestureActions.Clear();
            TrainedGesture.GestureAlgorithms.Clear();

            foreach (ListViewItem lvItem in lvAlgorithms.Items)
            {
                IGestureAlgorithm algorithm = (IGestureAlgorithm)lvItem.Tag;
                TrainedGesture.GestureAlgorithms.Add(algorithm);
            }

            foreach (ListViewItem lvItem in lvActions.Items)
            {
                IGestureAction action = (IGestureAction)lvItem.Tag;
                TrainedGesture.GestureActions.Add(action);
            }
            
            DialogResult = DialogResult.OK;
        }

        private void btnAlgorithmDown_Click(object sender, EventArgs e)
        {
            lvAlgorithms.MoveListViewItems(ListViewExtensions.Direction.Down);
        }

        private void btnAlgorithmUp_Click(object sender, EventArgs e)
        {
            lvAlgorithms.MoveListViewItems(ListViewExtensions.Direction.Up);
        }

        private void btnActionDown_Click(object sender, EventArgs e)
        {
            lvActions.MoveListViewItems(ListViewExtensions.Direction.Down);
        }

        private void btnActionUp_Click(object sender, EventArgs e)
        {
            lvActions.MoveListViewItems(ListViewExtensions.Direction.Up);
        }

        private void btnActionRemove_Click(object sender, EventArgs e)
        {
            lvActions.RemoveListViewItems();
        }

        private void btnAlgorithmRemove_Click(object sender, EventArgs e)
        {
            lvAlgorithms.RemoveListViewItems();
        }

        private void AddEditTrainedGestureForm_Load(object sender, EventArgs e)
        {
            foreach (IGestureAlgorithm algorithm in GestureLib.AvailableGestureAlgorithms)
            {
                NamedComboBoxItem namedComboBoxItem = new NamedComboBoxItem();
                namedComboBoxItem.NamedItem = algorithm;

                cmbAlgorithms.Items.Add(namedComboBoxItem);
            }

            foreach (IGestureAction action in GestureLib.AvailableGestureActions)
            {
                NamedComboBoxItem namedComboBoxItem = new NamedComboBoxItem();
                namedComboBoxItem.NamedItem = action;

                cmbActions.Items.Add(namedComboBoxItem);
            }


            if (TrainedGesture != null)
            {
                txtTrainedGestureName.Text = TrainedGesture.Name;

                foreach (IGestureAction action in TrainedGesture.GestureActions)
                { 
                    lvActions.Items.Add(CreateListViewItem(action));
                }

                foreach (IGestureAlgorithm algorithm in TrainedGesture.GestureAlgorithms)
                { 
                    lvAlgorithms.Items.Add(CreateListViewItem(algorithm));
                }
            }
        }

        private ListViewItem CreateListViewItem(INamed named)
        {
            ListViewItem lvItem = new ListViewItem(named.Name);
            lvItem.SubItems.Add(named.GetType().Name);
            lvItem.Tag = named;

            return lvItem;
        }

        private void btnActionAdd_Click(object sender, EventArgs e)
        {
            if (cmbActions.SelectedIndex > -1)
            {
                NamedComboBoxItem namedComboBoxItem = (NamedComboBoxItem)cmbActions.SelectedItem;
                ListViewItem lvItem = CreateListViewItem(namedComboBoxItem.NamedItem);
                lvActions.Items.Add(lvItem);
            }
        }

        private void btnAlgorithmAdd_Click(object sender, EventArgs e)
        {
            if (cmbAlgorithms.SelectedIndex > -1)
            {
                NamedComboBoxItem namedComboBoxItem = (NamedComboBoxItem)cmbAlgorithms.SelectedItem;
                ListViewItem lvItem = CreateListViewItem(namedComboBoxItem.NamedItem);
                lvAlgorithms.Items.Add(lvItem);
            }
        }
    }
}
